package activity

import history.*
import code.*

class Event implements Comparable {
    String name
    Kind kind
    String audience = "幼稚園,國小,國中,高中職,大學,成人,高齡"
    Date dateStart = new Date()
    Date dateEnd = new Date()
    //String place
    Place place
    String intro
    String cohost
    String partner
    String files
    String memo
    String custom1
    String custom2
    
    String certiScore
    String schalarScore
    String unit
    String courseCode
    Integer virtualHour
    Integer realHour
    String county
    String studyType
    Integer regFee = 0
    
    String status
    Integer capacityQty = 0
    Integer standbyQty = 0
    Integer registerQty = 0
    Integer registerLeft = 0
    Integer checkInQty //報到人數
    Integer registerHigh = 1 //最低報名人數
    Integer registerLow = 1//最高報名人數
    Date regStart = new Date() //報名起始
    Date regEnd = new Date() //報名截止
    String registerOnly //可否現場報名

    String checkIn //報到方式
//    Date checkInDate
    Date checkInStart
    Date checkInEnd
    String isAudit
    String isFood
    String isSleep
    String isCancel //是否取消

    History history
    static belongsTo = [serial:Serial]
    Field field
    static hasMany = [registers:Register]
    
    static constraints = {
        serial(blank:false)
        kind(blank:false)
        name(blank:false)
        audience(blank:false)
        dateStart(nullable:true)
        dateEnd(nullable:true)
        place(nullable:true)
        intro(nullable:true,size:0..5000)
        cohost(nullable:true)
        partner(nullable:true)
        files(nullable:true)
        memo(nullable:true,size:0..5000)
        custom1(nullable:true)
        custom2(nullable:true)
        
        certiScore(nullable:true)
        schalarScore(nullable:true)
        unit(nullable:true)
        courseCode(nullable:true)
        virtualHour(nullable:true)
        realHour(nullable:true)
        county(nullable:true)
        studyType(nullable:true)
        regFee(nullable:true)
        
        status(nullable:true)
        capacityQty(nullable:true)
        standbyQty(nullable:true)
        registerQty(nullable:true)
        checkInQty(nullable:true)
        registerHigh(nullable:true)
        registerLow(nullable:true)
        regStart(nullable:true)
        regEnd(nullable:true)
        registerOnly(inList:["Y", "N"],nullable:true)

        checkIn(nullable:true)
//        checkInDate(nullable:true)
        checkInStart(nullable:true)
        checkInEnd(nullable:true)
        isAudit(inList:["Y", "N"],nullable:true)
        isFood(inList:["Y", "N"],nullable:true)
        isSleep(inList:["Y", "N"],nullable:true)
        isCancel(inList:["Y", "N"],nullable:true)
        
        field(nullable:true)
        history(nullable:true)
    }
    
    String toString() {
        name
    }
    
    int compareTo(o) {
        dateStart.compareTo(o.dateStart)
    }    
    
    def calcRegisterQty() {
//write in RegisterController.calcRegisterQty()
//        newRegisterQty = 0
//        for (register in registers) {
//            if (register.present.equals("未出席") && ['正取報名','候補報名','候補補到','待審報名','報名審過'].contains(register.status)) {
//                newRegisterQty += 1
//            }
//        }
//        registerQty = newRegisterQty
//        updateStatus()
//        this.save()
    }
    
    def updateStatus() {
        if (isCancel == "Y") {
            status = "場次取消"
        } else if(new Date().time < regStart?.time) {//早於報名，尚未開放
            status = "尚未開放"
        } else if (new Date().time > regEnd?.time) {//晚於報名，報名截止
            status = "報名截止"
        } else if (registerQty < capacityQty) {//人數小於正式名額，尚可報名
            status = "尚可報名"
        } else if (registerQty >= capacityQty && registerQty < capacityQty + standbyQty) {//人數小於，名額+候補，尚可報名
            status = "候補報名"
        } else if (registerQty >= capacityQty + standbyQty) {//人數等於，名額+候補，報名額滿
            status = "報名額滿"
        } else {//"特殊狀態"
        }
        serial.updateFields()
    }
}